<?php

/**
 * 订单类
 * Date: 2018/11/21
 */

namespace Lib\Paas;

class Order extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 生成订单
     * @param array $data =
     * [
     *      'payActId' => '支付配置ID，请联系开发配置',
     *      'payPropId' => '支付礼包ID，请联系开发配置（含金额与道具配置）',
     *      'payAppId' => '支付应用ID',
     *      'payChannel' => '支付渠道ID',
     *      'payFrom' => '支付来源：h5|pc',
     *      'payType' => '1-游戏一级代币，2-人民币，3-游戏二级代币',
     *      'area' => '服务器ID（或者手游渠道：手Q、微信)',
     *      'platId' => '平台ID（IOS或者Android系统对应的ID）'
     *      'partition' => '小区ID',
     *      'roleId' => '角色ID',
     *      'roleName' => '角色昵称，编码方式：urlencode，编码：utf8',
     * ]
     * @param array $sData = 
     * [ 
     *      '自定义key' => '自定义value'
     * ]
     * @return array
     * [
     *      'iRet' => '=0 成功，!=0 失败',
     *      'sMsg' => '接口返回提示信息',
     *      'data' => [
     *          'serial' => '订单号',
     *          'act_amount' => '物品数量',
     *          'dc' => '支付方式',
     *          'event_id' => '事件ID',
     *          'iPayType' => '支付类型',
     *          'serverTime' => '系统时间',
     *      ]
     * ]
     */
    public function generateOrder($data, $sData = array())
    {
        $url = $this->getPaasRequestUrl('order.placeorder', $data);

        if ($sData) {
            $postData = json_encode($sData);
            $res = $this->http->post($url, $postData);
        } else {
            $res = $this->http->get($url);
        }
        recordLog('生成订单接口返回数据：' . var_export($res, true), __FILE__, __LINE__);
        if ($res['ret'] != 0) {
            $this->tglogReport('框架请求ULink-Paas出现异常，curl_errno：' . $res['ret'] . '，curl_error：' . $res['msg'], 'order.placeorder');
            return $this->getRetData(CURL_ERROR_NO, '系统繁忙，请稍后再试！');
        }
        $res = $res['data'];
        if ($res['ret'] != 0) {
            return $this->getRetData($res['ret'], '系统繁忙，请稍后再试！');
        } else {
            return $this->getRetData(0, 'success', ['data' => $res]);
        }
    }

    /**
     * 订单详情
     * @param string $orderId 订单号，由下单接口获得
     * @return array
     * [
     *      'iRet' =>'返回码',
     *      'sMsg' =>'系统消息',
     *      'data' => [
     *          'iStatus' => '订单状态：1待付款，2已付款,发货中，3已发货,交易完成，-2系统取消，-1用户取消',
     *           ...
     *      ]
     * ]
     */
    public function orderDetail($orderId)
    {
        $paasParam = ['serial' => $orderId];

        //url
        $url = $this->getPaasRequestUrl('order.detail', $paasParam);
        $res = $this->http->get($url);
        recordLog('订单详情接口返回数据：' . var_export($res, true), __FILE__, __LINE__);
        if ($res['ret'] != 0) {
            $this->tglogReport('框架请求ULink-Paas出现异常，curl_errno：' . $res['ret'] . '，curl_error：' . $res['msg'], 'order.orderdetail');
            return $this->getRetData(CURL_ERROR_NO, '系统繁忙，请稍后再试！');
        }
        $res = $res['data'];
        if ($res['ret'] != 0) {
            return $this->getRetData($res['ret'], '系统繁忙，请稍后再试！');
        } else {
            return $this->getRetData(0, 'success', $res['data']);
        }
    }

    /**
     * 订单列表
     * @param string $userId   下单账号openid/qq号
     * @param int $page     页号，默认为1
     * @param int $pageSize     每页条目数，默认为10
     * @return array ['ret' =>'返回码', 'msg' =>'系统消息', 'data' => '订单列表']
     */
    public function orderList($userId, $page = 1, $pageSize = 10)
    {
        $paasParam = ['user' => $userId, '' => $page, 'pageSize' => $pageSize];

        //url
        $url = $this->getPaasRequestUrl('order.orderlist', $paasParam);
        $res = $this->http->get($url);
        recordLog('订单列表接口返回数据：' . var_export($res, true), __FILE__, __LINE__);
        if ($res['ret'] != 0) {
            $this->tglogReport('框架请求ULink-Paas出现异常，curl_errno：' . $res['ret'] . '，curl_error：' . $res['msg'], 'order.orderlist');
            return $this->getRetData(CURL_ERROR_NO, '系统繁忙，请稍后再试！');
        }
        $res = $res['data'];
        if ($res['ret'] != 0) {
            return $this->getRetData($res['ret'], '系统繁忙，请稍后再试！');
        } else {
            return $this->getRetData(0, 'success', $res['data']);
        }
    }
}
